package site.jlopen.kuscia.rpc.impl;

import java.io.FileNotFoundException;

import org.secretflow.v1alpha1.factory.KusciaAPIChannelFactory;
import org.secretflow.v1alpha1.kusciaapi.DomainDataServiceGrpc;
import org.secretflow.v1alpha1.kusciaapi.DomainDataServiceGrpc.DomainDataServiceBlockingStub;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.BatchQueryDomainDataRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.BatchQueryDomainDataResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.CreateDomainDataRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.CreateDomainDataResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.DeleteDomainDataRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.DeleteDomainDataResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.ListDomainDataRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.ListDomainDataResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.QueryDomainDataRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.QueryDomainDataResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.UpdateDomainDataRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindata.UpdateDomainDataResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import site.jlopen.kuscia.rpc.KusciaDomainDataRpc;
import site.jlopen.kuscia.exception.SdkBaseException;
import site.jlopen.kuscia.properties.KusciaAPIProperties;

import io.grpc.ManagedChannel;

public class KusciaDomainDataRpcImpl implements KusciaDomainDataRpc {
	private static Logger logger = LoggerFactory.getLogger(KusciaDomainDataRpcImpl.class);

	private DomainDataServiceBlockingStub blockingStub;
	private ManagedChannel channel;

	public KusciaDomainDataRpcImpl(KusciaAPIProperties properties) {
		try {
			KusciaAPIChannelFactory channelFactory = new KusciaAPIChannelFactory(properties.getAddress(),
					properties.getTokenFile(), properties.getTls(), properties.getProtocol());
			this.channel = channelFactory.newClientChannel();
			this.blockingStub = DomainDataServiceGrpc.newBlockingStub(channel);
		} catch (Exception e) {
			if(e instanceof FileNotFoundException) {
				throw SdkBaseException.of("证书文件不存在", e);
			}
			throw SdkBaseException.of("Channel通讯异常", e);
		}
	}

	@Override
	public CreateDomainDataResponse createDomainData(CreateDomainDataRequest request) {
		try {
			CreateDomainDataResponse response = blockingStub.createDomainData(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public UpdateDomainDataResponse updateDomainData(UpdateDomainDataRequest request) {
		try {
			UpdateDomainDataResponse response = blockingStub.updateDomainData(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public DeleteDomainDataResponse deleteDomainData(DeleteDomainDataRequest request) {
		try {
			DeleteDomainDataResponse response = blockingStub.deleteDomainData(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public QueryDomainDataResponse queryDomainData(QueryDomainDataRequest request) {
		try {
			QueryDomainDataResponse response = blockingStub.queryDomainData(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public BatchQueryDomainDataResponse batchQueryDomainData(BatchQueryDomainDataRequest request) {
		try {
			BatchQueryDomainDataResponse response = blockingStub.batchQueryDomainData(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public ListDomainDataResponse listDomainData(ListDomainDataRequest request) {
		try {
			ListDomainDataResponse response = blockingStub.listDomainData(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

}
